package top.zhangjianyong.tools.indicator.strategy.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.zhangjianyong.tools.indicator.context.IndicatorContext;
import top.zhangjianyong.tools.indicator.model.TechnicalIndicatorResult;

import java.math.BigDecimal;

/**
 * 均线交叉信号指标计算器
 * 计算SMA5与SMA20交叉、EMA5与EMA20交叉（金叉/死叉）
 *
 * @author zhangjianyong
 * @date 2024/01/01
 */
@Component
public class MovingAverageCrossIndicator extends AbstractIndicator {

    private static final String INDICATOR_NAME = "MA_CROSS";

    @Autowired
    private SMAIndicator smaIndicator;

    @Autowired
    private EMAIndicator emaIndicator;

    @Override
    public String getName() {
        return INDICATOR_NAME;
    }

    @Override
    public int getMinDataSize() {
        return 20; // 需要20日均线
    }

    @Override
    protected TechnicalIndicatorResult doCalculate(IndicatorContext context) {
        TechnicalIndicatorResult result = TechnicalIndicatorResult.success(getName());

        // 计算SMA交叉信号
        TechnicalIndicatorResult smaResult = smaIndicator.calculate(context);
        if (smaResult.isSuccess()) {
            BigDecimal sma5 = smaResult.getBigDecimalValue("sma_5");
            BigDecimal sma20 = smaResult.getBigDecimalValue("sma_20");

            if (sma5 != null && sma20 != null) {
                int smaCrossSignal = calculateCrossSignal(sma5, sma20);
                result.addValue("sma_cross_signal", smaCrossSignal);
                result.addValue("sma_cross_type", getCrossType(smaCrossSignal));
            }
        }

        // 计算EMA交叉信号
        TechnicalIndicatorResult emaResult = emaIndicator.calculate(context);
        if (emaResult.isSuccess()) {
            BigDecimal ema5 = emaResult.getBigDecimalValue("ema_5");
            BigDecimal ema20 = emaResult.getBigDecimalValue("ema_20");

            if (ema5 != null && ema20 != null) {
                int emaCrossSignal = calculateCrossSignal(ema5, ema20);
                result.addValue("ema_cross_signal", emaCrossSignal);
                result.addValue("ema_cross_type", getCrossType(emaCrossSignal));
            }
        }

        return result;
    }

    /**
     * 计算交叉信号
     * 1: 金叉（短期均线上穿长期均线）
     * -1: 死叉（短期均线下穿长期均线）
     * 0: 无交叉
     */
    private int calculateCrossSignal(BigDecimal shortMA, BigDecimal longMA) {
        int comparison = shortMA.compareTo(longMA);

        // 简化实现：只判断当前状态
        // 完整实现需要比较前一日和当前日的状态来判断是否发生交叉
        if (comparison > 0) {
            return 1; // 短期均线在长期均线上方，视为金叉状态
        } else if (comparison < 0) {
            return -1; // 短期均线在长期均线下方，视为死叉状态
        } else {
            return 0; // 无交叉
        }
    }

    /**
     * 获取交叉类型描述
     */
    private String getCrossType(int signal) {
        switch (signal) {
            case 1:
                return "金叉";
            case -1:
                return "死叉";
            default:
                return "无交叉";
        }
    }
}

